package com.pkk.penguin.frame.know.java.interview.singleton;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
import org.junit.Test;

/**
 * @description: 单例模式测试
 * @author: peikunkun
 * @create: 2019-12-28 17:34
 **/
public class SingletonTest {

  @Test
  public void kunzai_19_20191228181149() throws ExecutionException, InterruptedException {
    System.out.println("欢迎使用单元测试方法【kunzai_19()_20191228181149】");
    System.out.println("此方法测试描述：【多线程-懒汉版单例模式】");

    //线程大小
    int threadSize = 10;

    ExecutorService executorService = Executors.newFixedThreadPool(threadSize);
    ArrayList<Future<SingletonInterview3>> futures = new ArrayList<>();
    for (int i = 0; i < threadSize; i++) {
      Callable<SingletonInterview3> callable = new Callable<SingletonInterview3>() {
        @Override
        public SingletonInterview3 call() throws Exception {
          return SingletonInterview3.getInstance();
        }
      };
      futures.add(executorService.submit(callable));
    }
    executorService.shutdown();

    HashSet<Object> hashSet = new HashSet<>();
    for (int i = 0, j = futures.size(); i < j; i++) {
      Future<SingletonInterview3> singletonInterview3Future = futures.get(i);
      System.out.println(singletonInterview3Future.get());
      hashSet.add(singletonInterview3Future.get().getClass().hashCode());
    }
    System.out.println(hashSet);
  }


  @Test
  public void kunzai_13_20191228173836() throws InterruptedException, ExecutionException {
    System.out.println("欢迎使用单元测试方法【kunzai_13()_20191228173836】");
    System.out.println("此方法测试描述：【懒汉式-单线程可以,多线程产生问题】");

    //线程大小
    int threadSize = 2;

    ExecutorService executorService = Executors.newFixedThreadPool(threadSize);
    ArrayList<Future<SingletonInterview2>> futures = new ArrayList<>();
    for (int i = 0; i < threadSize; i++) {
      Callable<SingletonInterview2> callable = new Callable<SingletonInterview2>() {
        @Override
        public SingletonInterview2 call() throws Exception {
          return SingletonInterview2.getInstance(true);
        }
      };
      futures.add(executorService.submit(callable));
    }
    executorService.shutdown();

    for (int i = 0, j = futures.size(); i < j; i++) {
      Future<SingletonInterview2> singletonInterview2Future = futures.get(i);
      System.out.println(singletonInterview2Future.get());
    }
  }


  @Test
  public void kunzai_15_20191228173251() {
    System.out.println("欢迎使用单元测试方法【kunzai_15()_20191228173251】");
    System.out.println("此方法测试描述：【饿汉式】");
    SingletonInterview1 singletonInterview1 = SingletonInterview1.INSTANCE;
    SingletonInterview1 sinletonInterview2 = SingletonInterview1.INSTANCE;

    System.out.println(singletonInterview1 == sinletonInterview2);
    System.out.println(singletonInterview1);
  }

}
